package com.smile.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smile.commons.page.MyBatisPlusPageConverter;
import com.smile.commons.page.R;
import com.smile.model.dto.request.*;
import com.smile.model.response.GetAirportByIdsResponse;
import com.smile.model.response.response.GetAirportByIdResponseDTO;
import com.smile.model.response.response.GetAirportByIdsResponseDTO;
import com.smile.model.response.response.GetAirportListResponseDTO;
import com.smile.model.request.*;
import com.smile.model.response.Drone;
import com.smile.model.response.GetAirportByIdResponse;
import com.smile.model.response.GetAirportListResponse;
import com.smile.service.AirportService;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/airport")
public class AirportController {

    @Autowired
    private AirportService airportService;

    @PostMapping(value = "/addAirport")
    public R<Void> addAirport(@Valid @RequestBody AddAirportRequest addAirportRequest, HttpServletRequest request) throws IOException {
        AddAirportRequestDTO addAirportRequestDTO = new AddAirportRequestDTO();
        BeanUtil.copyProperties(addAirportRequest, addAirportRequestDTO);
        Boolean result = airportService.addAirport(addAirportRequestDTO);
        if (result) {
            return R.ok();
        } else {

            return R.fail();
        }
    }

    @PostMapping("/getAirportDetailInfo")
    public R getAirportDetailInfo(@RequestBody GetAirportDetailInfoRequest getAirportDetailInfoRequest) {
        return R.ok();
    }

    @PostMapping("/getAirportById")
    public R<GetAirportByIdResponse> getAirportById(@RequestBody GetAirportByIdRequest getAirportByIdRequest) {
        GetAirportByIdRequestDTO getAirportByIdRequestDTO = BeanUtil.copyProperties(getAirportByIdRequest, GetAirportByIdRequestDTO.class);
        GetAirportByIdResponseDTO getAirportByIdResponseDTO = airportService.getAirportById(getAirportByIdRequestDTO);
        if (Objects.isNull(getAirportByIdResponseDTO)) {
            return R.fail();
        }else{
            GetAirportByIdResponse getAirportByIdResponse = BeanUtil.copyProperties(getAirportByIdResponseDTO, GetAirportByIdResponse.class);
            if (!Objects.isNull(getAirportByIdResponseDTO.getDroneDTOs())) {
                getAirportByIdResponse.setDroneList(BeanUtil.copyToList(getAirportByIdResponseDTO.getDroneDTOs(), Drone.class));
            }
            return R.ok(getAirportByIdResponse);
        }

    }

    @PostMapping("/getAirportByIds")
    public R<Map<Long, GetAirportByIdsResponse>> getAirportByIds(@RequestBody List<Long> ids) {
        Map<Long, GetAirportByIdsResponseDTO> getAirportByIdsResponseDTOS = airportService.getAirportByIds(ids);
        Map<Long, GetAirportByIdsResponse> map = new HashMap<>();
        getAirportByIdsResponseDTOS.forEach((i1, i2) -> {
            map.put(i1, BeanUtil.copyProperties(i2, GetAirportByIdsResponse.class));
        });
        return R.ok(map);
    }

    @PostMapping(value = "/editAirport")
    public R<Void> editAirport(@Valid @RequestBody EditAirportRequest editAirportRequest, HttpServletRequest request) throws IOException {
        EditAirportRequestDTO editAirportRequestDTO = new EditAirportRequestDTO();
        BeanUtil.copyProperties(editAirportRequest, editAirportRequestDTO);
        Boolean result = airportService.editAirport(editAirportRequestDTO);
        if (result) {
            return R.ok();
        } else {
            return R.fail("机场名称已存在");
        }
    }

    @PostMapping("/deleteAirport")
    public R<Void> deleteAirport(@RequestBody Long id) {
//        DeleteAirportRequestDTO deleteAirportRequestDTO = BeanUtil.copyProperties(deleteAirportRequest, DeleteAirportRequestDTO.class);
        Boolean result = airportService.deleteAirport(id);
        if (result) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @PostMapping("/deleteAirportByOrganizationId")
    public R<Void> deleteAirportByOrganizationId(@RequestBody Long id) {
//        DeleteAirportRequestDTO deleteAirportRequestDTO = BeanUtil.copyProperties(deleteAirportRequest, DeleteAirportRequestDTO.class);
        Boolean result = airportService.deleteAirportByOrganizationId(id);
        if (result) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @PostMapping("/getAirportList")
    public R<Page<GetAirportListResponse>> getAirportList(@RequestBody GetAirportListRequest getAirportListRequest) {
        GetAirportListRequestDTO getAirportListRequestDTO = new GetAirportListRequestDTO();

        BeanUtil.copyProperties(getAirportListRequest, getAirportListRequestDTO);
        Page<GetAirportListResponseDTO> pages = airportService.getAirportList(getAirportListRequestDTO);

        return R.ok(MyBatisPlusPageConverter.convertPage(pages, GetAirportListResponse.class));
    }

    @PostMapping("/getAirportList1")
    public R<List<GetAirportListResponse>> getAirportList1(@RequestBody GetAirportListRequest1 getAirportListRequest) {
        GetAirportListRequestDTO getAirportListRequestDTO = new GetAirportListRequestDTO();

        BeanUtil.copyProperties(getAirportListRequest, getAirportListRequestDTO);
        List<GetAirportListResponseDTO> pages = airportService.getAirportList1(getAirportListRequestDTO);

        return R.ok(BeanUtil.copyToList(pages, GetAirportListResponse.class));
    }

}
